/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.TbAdvertise;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.AdvertiseService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户Controller
 * @author lishuai
 * @version 2016-10-17
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/advertise")
public class AdvertiseController extends BaseController {
    @Autowired
    private AdvertiseService advertiseService;

    @ModelAttribute
    public TbAdvertise get(@RequestParam(required=false) Integer id) {
        if (null!=id){
            return advertiseService.get(id);
        }else{
            return new TbAdvertise();
        }
    }
    @RequiresPermissions("sys:advertise:view")
    @RequestMapping(value = "form")
    public String form(TbAdvertise advertise, Model model) {
        model.addAttribute("advertise", advertise);
        return "modules/sys/advertiseForm";
    }
    @RequiresPermissions("sys:advertise:view")
    @RequestMapping(value = {"advertiseList", ""})
    public String advertiseList(TbAdvertise advertise, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<TbAdvertise> page = advertiseService.findAdvertiseByPage(new Page<TbAdvertise>(request, response),advertise);
        model.addAttribute("page", page);
        if(page.getList().size()==0){
            model.addAttribute("message1","无查询结果");
        }
        return "modules/sys/advertiseList";
    }
    @RequiresPermissions("sys:advertise:edit")
    @RequestMapping(value = "advertiseEdit")
    public String advertiseEdit(TbAdvertise advertise, Model model,RedirectAttributes redirectAttributes) {
        User currentUser = UserUtils.getUser();
        if (StringUtils.isNotBlank(advertise.getTitle())){
            advertiseService.saveOrUpdateAdvertise(advertise);
            model.addAttribute("message", "保存广告信息成功");
        }
        addMessage(redirectAttributes, "编辑广告成功");
//        return "redirect:" + adminPath + "/sys/advertise/advertiseList/?repage";
        return "redirect:advertiseList";
    }

    @RequiresPermissions("sys:advertise:edit")
    @RequestMapping(value = "delete")
    public String delete(TbAdvertise advertise, RedirectAttributes redirectAttributes) {
        if(Global.isDemoMode()){
            addMessage(redirectAttributes, "演示模式，不允许操作！");
            return "redirect:" + adminPath + "/sys/advertise/advertiseList/?repage";
        }
        advertiseService.delete(advertise);
        addMessage(redirectAttributes, "删除广告成功");
        return "redirect:" + adminPath + "/sys/advertise/advertiseList/?repage";
    }
}
